﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace Jerry.DroidBuilder.Common
{
    [Serializable]
    public abstract class NotifyPropertyChangedObject : INotifyPropertyChanged
    {
        [field:NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected void OnPropertyChanged<TP>(Expression<Func<TP>> property)
        {
            var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;

            if (null == propertyInfo)
            {
                throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
            }

            OnPropertyChanged(propertyInfo.Name);
        }
    }
}
